package test.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import test.domain.zsjh;
import test.domain.zsjhExample;
import test.mapper.zsjhMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by libin on 2018/6/2.
 */
@Controller
public class QueryZSJHController {
    @Resource
    zsjhMapper zsjhMapper;

    @GetMapping("/beforeZSJH")
    public String beforeZSJH(Model model,@Param("nf") String nf) {

        zsjhExample example = new zsjhExample();
        zsjhExample.Criteria criteria = example.createCriteria();
        criteria.andNfEqualTo(nf);
        List<zsjh> results = zsjhMapper.selectByExample(example);
        model.addAttribute("results", results);
        int count = 0;
        for (zsjh z : results) {
            count += Integer.valueOf(z.getZsjh());
        }
        model.addAttribute("count", count);
        if(nf.equals("2018"))
           return "query/beforeZSJH";
        else return "query/beforeZSJH2019";
    }

    @RequestMapping("/ZSJH")
    public String ZSJH(Model model, @Param("sf") String sf, @Param("zy") String zy,@Param("nf") String nf) {

        zsjhExample example = new zsjhExample();
        zsjhExample.Criteria criteria = example.createCriteria();
        if ("所有省份".equals(sf)) ;
        else
            criteria.andSfEqualTo(sf);
        if ("".equals(zy)) ;
        else
            criteria.andZymcLike("%" + zy + "%");
        System.out.println("------------------------"+nf);
        criteria.andNfEqualTo(nf);
        List<zsjh> results = zsjhMapper.selectByExample(example);
        model.addAttribute("results", results);
        model.addAttribute("zy", zy);
        model.addAttribute("sf", sf);
        System.out.println(results.size());
        int count = 0;
        for (zsjh z : results) {
            count += Integer.valueOf(z.getZsjh());
        }
        model.addAttribute("count", count);
        if(nf.equals("2018"))
        return "query/beforeZSJH";
        else return "query/beforeZSJH2019";
    }

}
